package com.lagou.application;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;


/*
* 创建一个servlet作为顶层servlet，实现了一个ApplicationContextAware 接口。
ApplicationContextAware接口中setApplicationContext方法让Spring容器传递自己生成的ApplicationContext给我们使用，
* 然后通过applicationContext获取到DispatcherServlet的实例。
 * */
@Component
public class ContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    //从applicationContext获取DispatcherServlet
    public static DispatcherServlet getDispatcherServlet() {
        return context.getBean(DispatcherServlet.class);
    }
}
